package tongcheng.com.tongcheng.base.main.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yanzhenjie.nohttp.RequestMethod;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import tongcheng.com.tongcheng.ConStans;
import tongcheng.com.tongcheng.R;
import tongcheng.com.tongcheng.base.BaseActivity;
import tongcheng.com.tongcheng.network.CallServer;
import tongcheng.com.tongcheng.network.MyRequest;
import tongcheng.com.tongcheng.network.OnCallBack;
import tongcheng.com.tongcheng.utils.Setting;
import tongcheng.com.tongcheng.utils.ShowToast;

public class LiuYanFanKuiActivity extends BaseActivity {
    @Bind(R.id.back)
    ImageView mBack;
    @Bind(R.id.title)
    TextView mTitle;
    @Bind(R.id.title_bar)
    RelativeLayout mBg;
    @Bind(R.id.bt_tiJiaoLiuYan)
    Button bt_tiJiaoLiuYan;
    @Bind(R.id.et_liuYanContent)
    EditText et_liuYanContent;


    @Override
    public Integer initResid() {
        return R.layout.activity_liu_yan_fan_kui;
    }

    @Override
    public void initCreate(Bundle savedInstanceState) {
        settitlebar(mBg);
        mTitle.setText("留言反馈");

        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        bt_tiJiaoLiuYan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String text = et_liuYanContent.getText().toString();
                if(TextUtils.isEmpty(text)){
                    ShowToast.showToast("留言不能为空");
                }else {
                    tiJiaoLiuYan(text);
                }

            }
        });
    }

    private void tiJiaoLiuYan(String content) {
        String text = et_liuYanContent.getText().toString();
        MyRequest request = new MyRequest(ConStans.LiuYanFanKuiUrl, RequestMethod.POST);
        request.add("user_id", Setting.getId());
        request.add("user_name", Setting.getUserName());
        request.add("user_phone", Setting.getPhone());
        request.add("user_content", content + "");
        request.add("user_email", "");
        request.add("adminremark", "");

        CallServer.getInstance().addPost(this, request, 23, new OnCallBack() {
            @Override
            public void OnSuccess(int what, String json, Object ject) {
                try {
                    JSONObject object = new JSONObject(json);
                    Log.e("留言结果",json);
                    Log.e("留言结果",object.getString("msg"));
                    if(object.getInt("status") == 1){
                        ShowToast.showToast(object.getString("msg"));
                        finish();
                    }else {
                        ShowToast.showToast(object.getString("msg"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

    }
}
